package com.itheima.behavior.server.impl;

import com.heima.model.common.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.behavior.pojos.ApCollection;
import com.heima.model.common.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ArticleBehaviorDTO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.common.user.pojo.ApUser;
import com.itheima.behavior.server.ApArticleBehaviorService;
import com.itheima.behavior.server.ApBehaviorEntryService;
import com.itheima.common.exception.CustException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
//            ResponseResult<List<Map<String, Boolean>>> responseResult = new ResponseResult<>();

            HashMap<String, Boolean> hashMap = new HashMap<>();
            hashMap.put("isfollow",false);
            hashMap.put("islike",false);
            hashMap.put("isunlike",false);
            hashMap.put("iscollection",false);

            return ResponseResult.okResult(hashMap);
        }
        Integer userId = user.getId();
        ApBehaviorEntry entry = mongoTemplate.findOne(Query.query(Criteria.where("refId").is(userId)), ApBehaviorEntry.class);
        if (entry==null){
            entry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        }
        //查询三种行为实体
        //1 收藏
        ApCollection collection = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto.getArticleId())), ApCollection.class);
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto.getArticleId())), ApLikesBehavior.class);
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto.getArticleId())), ApUnlikesBehavior.class);
        HashMap<String, Boolean> hashMap = new HashMap<>();
        if (collection==null){
            hashMap.put("iscollection",false);
        }else {
            hashMap.put("iscollection",true);

        }
        if (likesBehavior==null){
            hashMap.put("islike",false);
        }else {
            hashMap.put("islike",true);
        }
        if (unlikesBehavior==null){
            hashMap.put("isunlike",false);
        }else {
            hashMap.put("isunlike",true);
        }
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + userId, String.valueOf(dto.getAuthorApUserId()));
        if (score==null){
            hashMap.put("isfollow",false);
        }{
            hashMap.put("isfollow",true);
        }

        return ResponseResult.okResult(hashMap);
    }
}
